package cn.sharp.bbs.controller.user;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.entity.DO.User;
import cn.sharp.bbs.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserControlller {
    @Resource
    UserService userService;

    /**
     * 获取用户简介信息
     * @param account 用户账号
     * @return 返回用户简介
     */
    @GetMapping("/{account}/brief")
    public Result getUserBrief(@PathVariable String account){
        if(userService.isInvalidAccount(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "此用户不存在");
        return Result.success(userService.getUserBrief(account));
    }

    /**
     * 获取用户基础统计信息
     * @param account 用户账号信息
     * @return 关注数量、粉丝数量、收藏数量、帖子数量、评论数量、积分总额、获赞数量等等
     */
    @GetMapping("/{account}/stats")
    public Result getUserStats(@PathVariable String account){
        if(userService.isInvalidAccount(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "此用户不存在");
        return Result.success(userService.getUserStats(account));
    }

    /**
     * 获取用户详细信息
     * @param account 用户账号, 需要验证与当前用户相同
     * @param user 当前用户
     * @return 用户信息详情
     */
    @GetMapping("/{account}/info")
    public Result getUserInfo(
            @RequestAttribute("CurrentUser") CurrentUser user,
            @PathVariable String account){
        if(user==null||!user.getU_account().equals(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        return Result.success(userService.getUserInfo(account));
    }

    /**
     * 修改用户详情 TODO: 在修改账号时自动重定向到新账号名
     * @param account 用户账号, 需要验证是当前用户
     * @param user 要修改的目标
     * @param currentUser 当前用户, 从Token中解析
     * @return 修改结果
     */
    @PostMapping("/{account}/info")
    public Result editUserInfo(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable String account, @RequestBody User user){
        if(currentUser==null||!currentUser.getU_account().equals(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(userService.isInvalidAccount(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "此用户不存在");
        user.setId(currentUser.getU_id());
        String payload = userService.doEditUser(user);
        if(payload!=null)
            return Result.fail(Result.ERR_CODE_BUSINESS, payload);
        else
            return Result.success();
    }

    /**
     * 对用户进行关注
     * @param currentUser 当前用户, 从Token中获得
     * @param account 要关注的用户id
     * @return 关注结果
     */
    @PostMapping("/{account}/follow")
    public Result followUser(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable String account){
        if(currentUser==null||currentUser.getU_name().equals(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(userService.isFollowed(currentUser.getU_id(), account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "不能重复关注");
        userService.followUser(currentUser.getU_id(), account);
        return Result.success();
    }

    /**
     * 取关用户
     * @param currentUser 当前用户, 从Token中获得
     * @param account 要取关的用户
     * @return 取关结果
     */
    @PostMapping("/{account}/unfollow")
    public Result unfollowUser(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable String account){
        if(currentUser==null||currentUser.getU_name().equals(account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(!userService.isFollowed(currentUser.getU_id(), account))
            return Result.fail(Result.ERR_CODE_BUSINESS, "未关注, 无法取关");
        userService.unfollowUser(currentUser.getU_id(), account);
        return Result.success();
    }

    @GetMapping("/{account}/follow")
    public Result checkFollow(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable String account
    ){
        if(currentUser==null||currentUser.getU_name().equals(account))
            return Result.success(false);
        else
            return Result.success(userService.isFollowed(currentUser.getU_id(), account));
    }
}
